package com.cet.recon.controller;

import jakarta.annotation.Resource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 30952
 */
@RestController
@RequestMapping("/job-launch")
public class JobLaunchController {

    @Resource
    private Job reconJob;


    @Resource
    private JobLauncher jobLauncher;

    @PostMapping("/recon")
    public Object recon() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
        JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
        jobParametersBuilder.addString("run.date", "2025-11-02");
        jobParametersBuilder.addLong("time", System.currentTimeMillis());
        JobExecution jobExecution = jobLauncher.run(reconJob, jobParametersBuilder.toJobParameters());
        return jobExecution.getExitStatus();
    }
}